const QQMapWX = require("../../utils/qqmap-wx-jssdk.min.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists: [], //公积金类型列表
    idx: 0, //下标
    city: "", //城市名字
    areacode: '', //城市code
    cuemsg: "城市获取中", //提示文字
    cueflag: true, //提示文字flag
    scrollto: '', //滚动值
    payfor: false
  }, 
  // 获取手机号
  getPhoneNumber(e) {
    console.log(e)
    let that = this;
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      this.nextBtn();
    } else {
      let iv = encodeURI(e.detail.iv)
      let encrypteddata = encodeURI(e.detail.encryptedData)
      let msg = {
        iv,
        encrypteddata
      }
      app.request('update_usertel', msg, true).then(res => {
        if (res.error_code == 0) {
          wx.setStorageSync('phone', true)
          this.setData({
            phone: true
          })
        }
        this.nextBtn();
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("首次渲染")
    // this.position()

    // 验证是否授权手机号
    let phone = wx.getStorageSync('phone');
    if (phone != "") {
      this.setData({
        phone: phone
      })
    } else {
      wx.setStorageSync('phone', false)
      this.setData({
        phone: false
      })
    }
    if (options.payfor && options.orderid) {
      this.setData({
        payfor: options.payfor,
        orderid: options.orderid
      })
    }
    wx.getNetworkType({
      success: function (res) {
        if (res.networkType == "none") {
          wx.showModal({
            title: '温馨提示',
            content: '当前网络不可用，请检查您的网络！',
            showCancel: false
          })
        }
      },
    })
    const that = this;
    let allMsg = wx.getStorageSync("allMsg")
    if (allMsg != "") {
      that.setData({
        allMsg: allMsg
      })
      if (allMsg.areacode) {
        app.request("area_by_prov", {
          area_code: allMsg.areacode
        }, true).then(res => {
          if (res.code == 200) {
            for (let i in res.data.area) {
              if (res.data.area[i].area_code == allMsg.areacode) {
                that.setData({
                  lists: res.data.area,
                  idx: allMsg.index ? allMsg.index : i,
                  areacode: allMsg.areacode,
                  city: res.data.prov_name,
                  cueflag: false,
                  "allMsg.index": allMsg.index ? allMsg.index : i,
                  scrollto: "a" + (allMsg.index ? allMsg.index : i)
                })
              }
            }
          }
        })
        return
      } else if (allMsg.province) {
        app.request("area_by_prov", {
          prov_name: allMsg.province
        }, true).then(res => {
          if (res.code == 200) {
            for (let i in res.data.area) {
              if (res.data.area[i].city_name == allMsg.city) {
                that.setData({
                  lists: res.data.area,
                  idx: i,
                  city: res.data.prov_name,
                  cueflag: false,
                  "allMsg.areacode": res.data.area[i].area_code,
                  "allMsg.index": allMsg.index ? allMsg.index : i,
                  scrollto: "a" + (allMsg.index ? allMsg.index : i)
                })
              } else {
                that.setData({
                  lists: res.data.area,
                  idx: 0,
                  city: res.data.prov_name,
                  cueflag: false,
                  "allMsg.areacode": res.data.area[0].area_code,
                  "allMsg.index": 0,
                  scrollto: "a0"
                })
              }
            }
          }
        })
        return
      }
    } else {
      console.log("第三个else")
      that.position()
    }
  },
  // 提取公积金时 点击确定
  extract() {
    let city = this.data.lists[this.data.idx].city_name;
    wx.navigateTo({
      url: `/pages/money_pinkup/pages/goldmsg/goldmsg?city=${city}&code=${this.data.areacode}&order_id=${this.data.orderid}`,
    })
  },
  //获取当前位置
  position: function () {
    let that = this;
    let qqmapsdk = new QQMapWX({
      key: "P56BZ-PYZ66-VLOSC-ER37Q-KSVO2-CTBD5"
    })
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (adressRes) {
            console.warn(adressRes)
            let cityname = adressRes.result.address_component.city;
            let a = cityname.replace("市", "");
            let b = a.replace("县", "");
            let province = adressRes.result.address_component.province; //获取的省
            that.setData({
              city: province,
              cityname: b,
              cueflag: false
            })
            that.area();
          },
          fail: function (res) {
            that.setData({
              cuemsg: "定位失败，请在网络良好的时候重试或直接手动选择城市"
            })
          },
        })
      },
      fail: function (res) {
        console.log("失败")

        that.setData({
          cuemsg: "定位失败，请检查位置权限或直接手动选择城市",
        })
      }
    })
  },
  area: function () {
    let that = this;
    wx.showLoading({
      title: '等待中',
    })
    app.request("area_by_prov", {
      prov_name: that.data.city
    }, true).then(res => {
      wx.hideLoading()
      if (res.code == 200) {
        for (let i in res.data.area) {
          let a = res.data.area[i];
          if (a.city_name == that.data.cityname) {
            let obj = {
              index: i,
              areacode: res.data.area[i].area_code
            }
            that.setData({
              idx: i,
              code: res.data.area[i].area_code,
              lists: res.data.area,
              scrollto: 'a' + i,
              allMsg: obj
            })
            wx.setStorageSync("allMsg", obj)
            return
          }
          else {
            let obj = {
              index: 0,
              areacode: res.data.area[0].area_code
            }
            that.setData({
              idx: 0,
              code: res.data.area[0].area_code,
              lists: res.data.area,
              scrollto: 'a0',
              allMsg: obj
            })
            wx.setStorageSync("allMsg", obj)
          }
        }
      }
    })
  },
  // 点击选择城市
  choosecity: function (e) {
    if (this.data.payfor) {
      wx.navigateTo({
        url: `../select/select?payfor=true&orderid=${this.data.orderid}`,
      })
    } else {
      wx.navigateTo({
        url: '../select/select',
      })
    }
  },
  // 公积金类型选中
  choose: function (e) {
    let index = e.target.dataset.index;
    let code = e.target.dataset.code;
    this.setData({
      idx: index,
      areacode: code,
      "allMsg.index": index,
      "allMsg.areacode": code
    })
    wx.setStorageSync("allMsg", this.data.allMsg)
  },
  // 点击下一步 开始查询
  nextBtn: function () {
    let areacode = this.data.allMsg.areacode;
    if (areacode != undefined) {
      wx.navigateTo({
        url: '../login/login?code=' + areacode,
      })
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '您需要选择公积金中心才能查询',
        showCancel: false
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "我正在查询公积金，快来查询吧",
      path: '/pages/index/index?channel=home',
      imageUrl: 'https://head.image.jiajijie.com/dj/images/wuxianyijn.png',
      success: function (res) {
        wx.showShareMenu({
          withShareTicket: true
        })
        console.log("成功")
        console.log(res)
      },
      fail: function (res) {
        console.log("失败")
        console.log(res)
      }
    }
  }
})